Page({
  data: {
    pageIndex: 1,
    tagsMap: [
      {key: 'all', rel: 'total', val: 1, title: '全部'},
      {key: 'has_cover', rel: 'cover', val: 1, title: '有图'},
      {key: 'has_video', rel: 'video', val: 1, title: '视频'},
      {key: 'has_again', rel: 'again', val: 1, title: '追加'},
      {key: 'explain_type', rel: 'good', val: 1, title: '好评'},
      {key: 'explain_type', rel: 'ordinary', val: 2, title: '中评'},
      {key: 'explain_type', rel: 'negative', val: 3, title: '差评'}
    ],
    tagsNum: [],
    tagKey: 'all',
    tagVal: '1',
    list: []
  },
  onLoad: function (options) {
    this.data.pid = options.pid;
    this.data.eid = options.eid;
    this.getEvaluateNum();
    this.getEvaluate();
  },
  onReachBottom: function () {
    if (this.data.noMore) return;
    this.getEvaluate();
  },
  getEvaluateNum: function () {
    wx.util.request('product.evaluateNum', {
      query: `id=${this.data.eid}`
    }).then(res => {
      this.setData({
        tagsNum: res
      });
    }).catch(err => {
      console.log(err);
    });
  },
  getEvaluate: function () {
    wx.util.request('', {
      query: `product_id=${this.data.pid}&page=${this.data.pageIndex}&${this.data.tagKey}=${this.data.tagVal}`
    }).then(res => {
      let list = this.data.list;
      // 格式化日期
      for (let item of res) {
        // 如果用户没有上传头像则使用系统默认
        if (!item.member_head_portrait) {
          item.member_head_portrait = '/static/images/user/no-login.png';
        }
        // 格式化用户名，除符合条件的字符外其余以“*”代替
        /*if (item.is_anonymous == 0 && item.member_nickname) {
          let first = '*', middle = '', last = '';
          if (item.member_nickname.length >= 2) {
            first = item.member_nickname.substr(0, 1);
            middle = item.member_nickname.substr(1, item.member_nickname.length - 2);
            last = '*';
            // 除4个及以上字符的昵称外，其余只保留第一个
            if (!middle || middle.length == 1) {
              last = '*' + (!middle ? '' : last);
            } else {
              let asterisk = '';
              // 方案一：按长度
              /*for (let i = 0; i < middle.length; i++) {
                asterisk += '*';
              }*/
              // 方案二：统一显示3个星号
              /*for (let i = 0; i < 3; i++) {
                asterisk += '*';
              }
              last = asterisk + item.member_nickname.substr(-1);
            }
          }
          item.member_nickname = first + last;
        }*/
        // 首评
        item.date = wx.time.toDate(item.created_at, 'Y-M-D');
        // 追评
        let days = parseInt((item.again_addtime - item.created_at) / 86400);
        item.days = days > 1 ? days : 1;
        list.push(item);
      }
      this.setData({
        pageIndex: this.data.pageIndex + 1,
        list: list,
        noMore: res.length < 10
      });
    }).catch(err => {
      console.log(err);
    });
  },
  tag: function (e) {
    let tag = this.data.tagsMap[e.currentTarget.dataset.index];
    this.setData({
      tagKey: tag['key'],
      tagVal: tag['val'],
      pageIndex: 1,
      list: []
    }, () => {
      this.getEvaluate();
    });
  },
  preview: function (e) {
    let id = e.currentTarget.id, // 评价记录集游标
        idx = e.currentTarget.dataset.idx, // 评价图组游标
        type = e.currentTarget.dataset.type, // 类型：首（追）评
        covers = this.data.list[id][type + 'covers'];
    if (typeof covers == 'string') {
      covers = [images];
      idx = 0;
    }
    if (covers.length < idx) {
      idx = covers.length;
    }
    wx.previewImage({
      urls: covers,
      current: covers[idx]
    });
  }
});